/*
 * FILENAME
 *     DaoServiceUtils.java
 *
 * FILE LOCATION
 *     $Source$
 *
 * VERSION
 *     $Id$
 *         @version       $Revision$
 *         Check-Out Tag: $Name$
 *         Locked By:     $Lockers$
 *
 * FORMATTING NOTES
 *     * Lines should be limited to 78 characters.
 *     * Files should contain no tabs.
 *     * Indent code using four-character increments.
 *
 */

package edu.haiu.training.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import edu.haiu.training.data.BaseEntity;

//
// IMPORTS
// NOTE: Import specific classes without using wildcards.
//

/**
 * <p>
 * Services utilities.
 * </p>
 *
 * @since 0.1
 *
 * @author nguyen.minh.tam
 **/
public class DaoServiceUtils
{
    private static final String SELECT_ALL = "Select * from ";
    
    @PersistenceContext
    private EntityManager entityManager;
    
    public <E extends BaseEntity> E save(final E entity)
    {
        return entityManager.merge(entity);
    }
    
    public <E extends BaseEntity> void delete(final E entity)
    {
        entityManager.remove(entity);
    }
    
    public <E extends BaseEntity> E findById(final Class<E> clazz,final  Long id)
    {
        return entityManager.find(clazz, id);
    }
    
    public <E extends BaseEntity> List<E> findAll(final Class<E> clazz)
    {
        return entityManager.createQuery(SELECT_ALL + clazz.getName()).getResultList();
    }
}
